package tgc.edu.fhh.web.controller;

import java.util.HashSet;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.fhh.custom.AjaxResult;
import tgc.edu.fhh.custom.TablePageable;
import tgc.edu.fhh.entity.Course;
import tgc.edu.fhh.service.CourseService;
import tgc.edu.fhh.web.form.CourseForm;

@Controller
@RequestMapping(value = "/course")
public class CourseController {
	@Autowired
	private CourseService courseService;

	@RequestMapping(value = "/manage")
	public void manage() {
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(CourseForm form) {
		Specification<Course> spec = buildSpec(form);
		return courseService.findAll(spec);
	}

	@RequestMapping(value = "/edit")
	public void edit(Integer id, ModelMap modelMap) {
		Course model = new Course();
		if (id != null) {
			model = courseService.findById(id);
		}
		modelMap.put("model", model);
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public Object save(CourseForm form) {
		try {
			Course model = new Course();
			Integer id = form.getId();
			if (id != null) {
				model = courseService.findById(id);
			}
			BeanUtils.copyProperties(form, model, "id");
			courseService.save(model);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
	}

	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(Integer id) {
		// courseService.deleteById(id);
		try {
			courseService.deleteById(id);
			return new AjaxResult("数据删除成功");
		} catch (Exception e) {
			return new AjaxResult(false, "数据删除失败");
		}

	}

	@RequestMapping(value = "/search")
	public void search(CourseForm form, ModelMap modelMap) {
		modelMap.put("model", form);
	}

	@SuppressWarnings("serial")
	private Specification<Course> buildSpec(CourseForm form) {
		Specification<Course> spec = new Specification<Course>() {

			@Override
			public Predicate toPredicate(Root<Course> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String name = form.getName();
				if (StringUtils.hasText(name)) {
					rules.add(cb.like(root.get("name"), "%" + name + "%"));
				}
				Integer classHour = form.getClassHour();
				if (classHour != null) {
					rules.add(cb.like(root.get("classHour").as(String.class), "%" + classHour + "%"));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	@RequestMapping(value = "/page")
	@ResponseBody
	public Object page(TablePageable tablePageable, CourseForm form) {
		PageRequest pageRequest = tablePageable.buildPageRequest();
		Specification<Course> spec = buildSpec(form);
		Page<Course> page = courseService.findAll(spec, pageRequest);
		return AjaxResult.buildTableResult(page);
	}
}
